// casedetail.js
import api from "../../../utils/api"
import util from "../../../utils/util"
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"2",
    collected:false,
    Case:{},
    talkList:[],
    talkText:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.isLogin();
    this.setData({
      id:options.id
    });
    this.getCaseDetail();

    // this.getTalks();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 收藏案例
   */
  saveCase:function(){
    console.log("点击收藏案例");
    var that = this;
    wx.request({
      url: api.casustrove,
      data:{
        id: app.globalData.openid,
        casus:this.data.id
      },
      method:"POST",
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      success:function(response){
        console.log("收藏/取消收藏案例成功！");
        console.log(response.data);
        var flag = response.data.infor;
        if (flag == "收藏案例成功"){
          that.setData({
            collected:true
          });
        }else{
          that.setData({
            collected: false
          });
        } 
      }
    })
  },
  /**
   * 获取案例详情
   */
  getCaseDetail:function(){
    var that = this;
    wx.request({
      url: api.getCaseDetail,
      data:{
        id: that.data.id,
      },
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(response){
        console.log("获取案例详情");
        console.log(response.data);
        that.setData({
          Case:response.data.data
        });
        that.getTalks();
      }
    })
  },
  /**
 * 获取评论信息
 *  getCaseTalks_xq: URLHeader + "qzComment",//查看相亲案例评论
    getCaseTalks_zx: URLHeader + "qzComment",//查看咨询案例评论
    getCaseTalks_hd: URLHeader + "qaComment",//查看活动案例评论
 */
  getTalks: function () {
    var that = this;
    var url = "";
    var flag = this.data.Case.genre;
    if(flag == "相亲"){
      url = api.getCaseTalks_xq;
    }else if(flag == "活动"){
      url = api.getCaseTalks_hd;
    }else if(flag == "咨询"){
      url = api.getCaseTalks_zx;
    }else if(flag == "教育"){
      url = api.getCaseTalks_hd;
    }
    wx.request({
      url: url,
      data: {
        id: that.data.id
        // id:1
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (response) {
        console.log("获取评论信息成功！");
        console.log(response.data);
        that.setData({
          talkList: response.data.data
        });
      }
    })
  },
  talkTextInput:function(e){
      console.log(e.detail.value);
      this.setData({
        talkText:e.detail.value
      });
  },
  /**
   * 发表评论
   */
  talk:function(){
    this.data.talkList.push({
      comment:this.data.talkText,
      id:app.globalData.openid,
      head:app.globalData.userHead,
      time: util.formatTime(new Date()),
      nickname:app.globalData.userName
    });
    this.setData({
      talkList: this.data.talkList,
      talkText:""
    });
    
  }
})